package com.zl.quickdoc.format.impl;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;

import com.zl.quickdoc.constant.QuickDocC;
import com.zl.quickdoc.constant.QuickDocEnum;
import com.zl.quickdoc.model.DocCfgParam;
import com.zl.quickdoc.model.DocChangeActionDesc;
import com.zl.quickdoc.model.DocChangeApis;
import com.zl.quickdoc.model.DocChangeFieldDesc;
import com.zl.quickdoc.model.DocChangeModuleDesc;

public class ApiFormatBase {
    
    public List<List<String>> joinChangeContent(List<DocChangeApis> changedata,String splitchar,String enterstr,String blankstr) throws Exception {
        List<List<String>> txt = new ArrayList<>();
        if(changedata == null || changedata.isEmpty()) {
            return txt;
        }
        
        for(DocChangeApis x:changedata) {
            List<String> txtvo = new ArrayList<>();
            txtvo.add(x.getVersion());
            txtvo.add(StringUtils.join(x.getRemark(), enterstr));
            txt.add(txtvo);
            if(x.getChanges()==null || x.getChanges().isEmpty()) {
                txtvo.add("");
                continue;
            }
            
            StringBuilder stringBuilder = new StringBuilder();
            int num = 1;
            for(DocChangeModuleDesc mx:x.getChanges()) {
                if(!QuickDocEnum.CHANGE_TYPE.CHANGE.code.equals(mx.getType())) {
                    stringBuilder.append(formatModuleDesc(num, mx.getDesc()));
                    stringBuilder.append(QuickDocEnum.CHANGE_TYPE.getMsg(mx.getType()));
                    stringBuilder.append(enterstr);
                    num++;
                    continue;
                }
                List<DocChangeActionDesc> actchildren = mx.getChildren();
                for(DocChangeActionDesc actx:actchildren) {
                    stringBuilder.append(formatModuleDesc(num, mx.getDesc()));
                    stringBuilder.append(QuickDocC.MODULE_ACTION_CHAR);
                    stringBuilder.append(String.format("%s %s：%s", actx.getDesc(), actx.getName(), QuickDocEnum.CHANGE_TYPE.getMsg(actx.getType())));
                    stringBuilder.append(enterstr);
                    if(!QuickDocEnum.CHANGE_TYPE.CHANGE.code.equals(actx.getType())) {
                        num++;
                        continue;
                    }
                    num++;
                    List<DocChangeFieldDesc> reqChildren = actx.getReqChildren();
                    if(reqChildren != null && !reqChildren.isEmpty()) {
                        stringBuilder.append(blankstr+QuickDocEnum.PARAM_TYPE.REQ.msg+"：");
                        for(DocChangeFieldDesc reqx:reqChildren) {
                            stringBuilder.append(formatFieldDesc(splitchar, reqx.getDesc(), reqx.getName(), QuickDocEnum.CHANGE_TYPE.getMsg(reqx.getType())));
                        }
                        stringBuilder.append(enterstr);
                    }
                    List<DocChangeFieldDesc> resChildren = actx.getResChildren();
                    if(resChildren != null && !resChildren.isEmpty()) {
                        stringBuilder.append(blankstr+QuickDocEnum.PARAM_TYPE.RES.msg+"：");
                        for(DocChangeFieldDesc resx:resChildren) {
                            stringBuilder.append(formatFieldDesc(splitchar, resx.getDesc(), resx.getName(), QuickDocEnum.CHANGE_TYPE.getMsg(resx.getType())));
                        }
                        stringBuilder.append(enterstr);
                    }
                }
            }
            txtvo.add(stringBuilder.toString());
        }
        return txt;
    }
    private String formatModuleDesc(int num,String desc) throws Exception {
       return String.format("%s、 %s",num, desc);
    }
    private String formatFieldDesc(String splitchar,String desc,String name,String type) throws Exception {
        return String.format("%s / %s（%s）"+splitchar, desc, name, type);
     }
    public File getTemplatePath(DocCfgParam p,String templateName) throws Exception {
        File tFile=null;
        if(StringUtils.isNotBlank(p.getTemplatePath())) {
            tFile = new File(p.getTemplatePath()+ File.separator + QuickDocC.CHILDREN_PATH_APIDOC + File.separator + templateName);
        }else {
            tFile = new File(System.getProperty("user.dir") + File.separator + QuickDocC.CHILDREN_PATH_APIDOC + File.separator + templateName);
        }
        return tFile;
     }
    
    public String getFileTitle(DocCfgParam p) throws Exception {
       return p.getProjectTitle()+(StringUtils.isNotBlank(p.getVersion())?"_"+p.getVersion():"")+"_";
    }
    public int getSplitNum(DocCfgParam p) throws Exception {
        Integer num=500;
        if(null!=p.getNum() && p.getNum()>0){
            num = p.getNum();
        }
        return num;
    }
    public void writeDoc(String s, FileOutputStream out2) throws Exception {
        try(FileOutputStream out = out2;) {
            IOUtils.write(s, out, StandardCharsets.UTF_8);
        } catch (IOException e) {
            System.out.println("接口文档写入文件失败, "+ e);
        }
    }
    
}
